package si.unimb.src.wifisms.ui;

import java.util.ArrayList;

import si.unimb.src.wifisms.R;
import si.unimb.src.wifisms.control.Database;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivityArrayAdapter extends ArrayAdapter<String> {

	private ArrayList<String> contentAll;
	private Context context;
	private int resourceId;
	private View v;
	
	public MainActivityArrayAdapter(Context context, int textViewResourceId, ArrayList<String> objects) {
		super(context, textViewResourceId, objects);
		this.context = context;
		this.contentAll = objects;
		this.resourceId = textViewResourceId;
	}
	
	/**
	 * Funkcija poveže podatke z View-i
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		v = convertView;

		/* nastavimo pravi layout */
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(this.resourceId, null);
		}
		
		// če ni računov je SendSMS menuItem pobarvan, disablea pa se preko Overridane isEnabled funkcije
		if (position == 0 && Database.getAccounts(context).isEmpty()) {
			v.setBackgroundColor(0xffcdcdc1);
		}
		
		String item = contentAll.get(position);
		if (item != null) {
			
			TextView listItem = (TextView)v.findViewById(R.id.MainListItemTextView);
			listItem.setText(item);
			
			ImageView iv = (ImageView)v.findViewById(R.id.MainListItemIcon);
			
			switch(position) {
				
				case 0: iv.setImageResource(R.drawable.sendsms_icon); break;
				case 1: iv.setImageResource(R.drawable.waitingmessages_icon); break;
				case 2: iv.setImageResource(R.drawable.accounts_icon); break;
				case 3: iv.setImageResource(R.drawable.settings_icon); break;
				case 4: iv.setImageResource(R.drawable.about_icon); break;
			}
		}
		
		return v;
	}
	
	/**
	 * Funkcija na določeni poziciji enablea/disablea listItem
	 */
	@Override
	public boolean isEnabled(int position) {
		if (position == 0 && Database.getAccounts(context).isEmpty()) {
			return false;
		} else
			return true;
	}
	
	/*@Override
	public boolean areAllItemsEnabled() {
	    return false;
	}*/
}
